import 'package:yp/config/config.dart';
import 'package:yp/helper.dart';
import 'package:yp/pages/main/logic.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import 'state.dart';

class CommoditySearchListLogic extends GetxController {
  // final MainAppLogic mainAppLogics = Get.put(MainAppLogic());
  final MainLogic mainLogic = Get.put(MainLogic());
  final state = CommoditySearchListState();

  @override
  void onInit() {
    super.onInit();

    // state.searchEditingController = TextEditingController();
    // state.searchFocusNode = FocusNode();

    state.singleDoubleRowBool = false;
    print(' Get.arguments   = ${Get.parameters}');
    state.catId = Get.parameters['linktarget'];
  }

  @override
  void onReady() {
    super.onReady();
    var params = {"cat_id": state.catId, "type": 2};
    Api.getClassificationName(params).listen((event) {
      if (event['code'] == 0) {
        state.titleString = event['cat_name'];
      }
      update();
    });

    String orderBy = '';
    if (state.chooseIndex == 0) {
      orderBy = '';
    } else if (state.chooseIndex == 1) {
      orderBy = 'sold_quantity desc';
    } else if (state.chooseIndex == 2) {
      orderBy = state.sortingBool ? 'price asc' : 'price desc';
    } else if (state.chooseIndex == 3) {
      orderBy = 'onsale_time desc';
    }
    var params1 = {
      // "shop_id": SpUtil.getInt('storeShopNum'),
      "shop_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      "virtual_cat_id": state.catId, //底层分类
      "virtual_parent_id": "21061000010004", //上级分类
      "use_platform": 2, //死值
      "page_no": state.page,
      "page_size": state.pageSize,
      'access_token': SpUtil.getString('token'),
      'customer_type': 3,
      'user_num_id': SpUtil.getInt('empeNumId'),
      'order_by': orderBy,
      // "prv_num_id": 320000, //省编码
      // "city_num_id": 320500,//市编码
      // "city_area_num_id": 320583 //区域编码
    };
    Api.goodsList(params1).listen((event) {
      print('分类商品列表 $event');
      if (event['code'] == 0) {
        //goodsList
        List list = event['lists']; // lists
        if (list.length > 0) {
          state.goodsList = list;
        } else {
          state.goodsList = [];
        }
      }
      update();
    });
  }

  // 下拉刷新
  void refreshData() {
    state.page = 1;
    state.goodsList = [];

    String orderBy = '';
    if (state.chooseIndex == 0) {
      orderBy = '';
    } else if (state.chooseIndex == 1) {
      orderBy = 'sold_quantity desc';
    } else if (state.chooseIndex == 2) {
      orderBy = state.sortingBool ? 'price asc' : 'price desc';
    } else if (state.chooseIndex == 3) {
      orderBy = 'onsale_time desc';
    }
    var params = {
      // "shop_id": SpUtil.getInt('storeShopNum'),
      "shop_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      "virtual_cat_id": state.catId, //底层分类
      "virtual_parent_id": "21061000010004", //上级分类
      "use_platform": 2, //死值
      "page_no": state.page,
      "page_size": state.pageSize,
      'access_token': SpUtil.getString('token'),
      'customer_type': 3,
      'user_num_id': SpUtil.getInt('empeNumId'),
      'order_by': orderBy,
      // "prv_num_id": 320000, //省编码
      // "city_num_id": 320500,//市编码
      // "city_area_num_id": 320583 //区域编码
    };
    Api.goodsList(params).listen((event) {
      print('分类商品列表 $event');
      if (event['code'] == 0) {
        //goodsList
        List list = event['lists']; // lists
        if (list.length > 0) {
          state.goodsList = list;
        } else {
          state.goodsList = [];
        }
      }
      state.controller1.refreshCompleted();
      update();
    });
  }

  // 上拉加载更多
  void loadMoreData() {
    state.page = state.page + 1;

    String orderBy = '';
    if (state.chooseIndex == 0) {
      orderBy = '';
    } else if (state.chooseIndex == 1) {
      orderBy = 'sold_quantity desc';
    } else if (state.chooseIndex == 2) {
      orderBy = state.sortingBool ? 'price asc' : 'price desc';
    } else if (state.chooseIndex == 3) {
      orderBy = 'onsale_time desc';
    }
    var params = {
      // "shop_id": SpUtil.getInt('storeShopNum'),
      "shop_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      "virtual_cat_id": state.catId, //底层分类
      "virtual_parent_id": "21061000010004", //上级分类
      "use_platform": 2, //死值
      "page_no": state.page,
      "page_size": state.pageSize,
      'access_token': SpUtil.getString('token'),
      'customer_type': 3,
      'user_num_id': SpUtil.getInt('empeNumId'),
      'order_by': orderBy,
      // "prv_num_id": 320000, //省编码
      // "city_num_id": 320500,//市编码
      // "city_area_num_id": 320583 //区域编码
    };
    Api.goodsList(params).listen((event) {
      print('分类商品列表 $event');
      List list1 = state.goodsList;
      if (event['code'] == 0) {
        List list = event['lists']; // lists
        if (list.length > 0) {
          list1.addAll(list);
          state.goodsList = list1;
        } else {
          state.page = state.page - 1;
        }
      }
      state.controller1.loadComplete();
      update();
    });
  }

  // 切换单双排
  void singleDoubleRowClick(bool isBool) {
    state.singleDoubleRowBool = isBool;
    update();
  }

  // 排序选择事件
  void chooseSortingClick(int index) {
    print(' 选择刷新事件   排序选择事件');
    if (index == 2) {
      state.sortingBool = !state.sortingBool;
    } else {
      state.sortingBool = true;
    }
    state.chooseIndex = index;

    String orderBy = '';
    if (state.chooseIndex == 0) {
      orderBy = '';
    } else if (state.chooseIndex == 1) {
      orderBy = 'sold_quantity desc';
    } else if (state.chooseIndex == 2) {
      orderBy = state.sortingBool ? 'price asc' : 'price desc';
    } else if (state.chooseIndex == 3) {
      orderBy = 'onsale_time desc';
    }
    var params1 = {
      // "shop_id": SpUtil.getInt('storeShopNum'),
      "shop_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      "virtual_cat_id": state.catId, //底层分类
      "virtual_parent_id": "21061000010004", //上级分类
      "use_platform": 2, //死值
      "page_no": state.page,
      "page_size": state.pageSize,
      'access_token': SpUtil.getString('token'),
      'customer_type': 3,
      'user_num_id': SpUtil.getInt('empeNumId'),
      'order_by': orderBy,
      // "prv_num_id": 320000, //省编码
      // "city_num_id": 320500,//市编码
      // "city_area_num_id": 320583 //区域编码
    };
    Api.goodsList(params1).listen((event) {
      print('分类商品列表 $event');
      if (event['code'] == 0) {
        //goodsList
        List list = event['lists']; // lists
        if (list.length > 0) {
          state.goodsList = list;
        } else {
          state.goodsList = [];
        }
      }
      update();
    });
    update();
  }

  /// 加入购物车
  void requestAddGoods(String ecShopNumId, String itemNumId, String styleNumId,
      String ecStyleNumId) {
    var params = {
      "use_platform": 2,
      "usr_num_id": SpUtil.getInt('empeNumId'),
      "emp_num_id": 0,
      "channel_num_id": 99,
      "quantity": 1.0,
      "ec_shop_num_id": ecShopNumId,
      "item_num_id": itemNumId,
      "style_num_id": styleNumId,
      "ec_style_num_id": ecStyleNumId,
      'access_token': SpUtil.getString('token'),
    };
    Api.addShoppingCart(params).listen((event) {
      if (event['code'] == 0) {
        mainLogic.getCartNum();
        toast('添加成功');
      } else {
        toast(event['message']);
      }
    });
  }
}
